#!/usr/bin/env -S python3 -B

from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
import subprocess as p
from typing import IO, Any, Generator
import requests

MY_NICK = 'pimenovdmitry'
REVIEWERS = 'https://gitee.com/openharmony/arkcompiler_ets_frontend/raw/master/ets2panda/REVIEWERS'

@dataclass
class Rule:
    pattern: str
    users: set[str]


def parse(line: str) -> Rule | None:
    line = line.strip()
    if len(line) == 0 or line[0] == '#':
        return None
    ar = line.split()
    if len(ar) > 1:
        return Rule(pattern=ar[0], users=set(ar[1:]))
    return None

@dataclass
class Pattern:
    mandatory: bool
    pattern: str

def get_my_patterns(lines: Sequence[str], name: str):
    for line in lines:
        rule = parse(line)
        if rule:
            if ('@' + name) in rule.users:
                yield Pattern(mandatory=True, pattern=rule.pattern)
            if ('^' + name) in rule.users:
                yield Pattern(mandatory=False, pattern=rule.pattern)


if __name__ == '__main__':
    resp = requests.get(REVIEWERS)
    patterns = list(get_my_patterns(resp.text.splitlines(), MY_NICK))
    
    for pattern in patterns:
        pat = pattern.pattern = pattern.pattern[1:] if pattern.pattern[0] == '/' else pattern.pattern
        print(('@' if pattern.mandatory else '^') + f' {pat}')
        for f in Path('arkcompiler/ets_frontend').glob(pat):
            print(f'  {f}')
